/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.dataaccess.dao.event.dao;

import com.nus.dataaccess.dao.base.dao.BaseDao;
import com.nus.dataaccess.dao.event.Event;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author
 */
public class EventDao extends BaseDao<Event>{

    private static EventDao instance;

    public static EventDao getInstance() {
        if (null == instance) {
            instance = new EventDao();
        }
        return instance;
    }
    @Override
    protected Class getReferenceClass() {return Event.class;}


    public List<Event> getAllEvents(){
       Criteria crit = super.createCriteria();
       List<Event> events = crit.list();
       return events;
    }

    public Event getEventById(Long eventId){
       Criteria crit = super.createCriteria();
       crit.add(Restrictions.eq("id", eventId));
       crit.setMaxResults(1);
       List<Event> events = crit.list();
       return (events!=null && !events.isEmpty()) ? events.get(0) : null;
    }

    public List<Event> getEventsByConferenceId(Long conferenceId){
       Criteria crit = super.createCriteria();
       crit.add(Restrictions.eq("conferenceId", conferenceId));
       crit.setMaxResults(1);
       List<Event> events = crit.list();
       return events;
    }

    public Event getEventByDescription(String desc){
       Criteria crit = super.createCriteria();
       crit.add(Restrictions.eq("desc", desc));
       crit.setMaxResults(1);
       List<Event> events = crit.list();
       return (events!=null && !events.isEmpty()) ? events.get(0) : null;
    }

    public int getTotalCount(Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        crit.createAlias("conference", "a");


        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                if("conferenceId".equals(key)){
                    crit.add(Restrictions.eq("a.id", filters.get(key)));
                }else{
                    crit.add(Restrictions.eq(key, filters.get(key)));
                }


            }
        }

        return ((Long)crit.list().get(0)).intValue();
    }

    public int getTotalCountByConferenceId(Long conferenceId, Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        crit.createAlias("conference", "a");
        crit.add(Restrictions.eq("a.id", conferenceId));

        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }

        return ((Long)crit.list().get(0)).intValue();
    }

    public List<Event> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters){
        Criteria crit = super.createCriteria();

        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                System.out.println("key "+key);
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }

        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
        return crit.list();
    }
}
